using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Sys;
using net_work.Data.Entity.Sys;
using net_work.Data.VO.Sys;
using net_work.IRepository.Sys;
using net_work.IService.Sys;

namespace net_work.Service.Sys;

public class CollegeService(ICollegeRepository collegeRepository, ITeacherRepository teacherRepository)
    : ICollegeService
{
    public Task<PagedResult<CollegeResult>> GetCollegesPagedAsync(PagedRequest request)
    {
        return collegeRepository.GetCollegePagedAsync(request);
    }

    public Task<College?> SelectByIdAsync(long collegeId)
    {
        return collegeRepository.GetByIdAsync(collegeId);
    }

    public async Task<long> InsertCollege(CollegeAddRequest collegeAddRequest)
    {
        switch (collegeAddRequest.DeanId)
        {
            case null:
                return await collegeRepository.InsertAsync(collegeAddRequest);
            case <= 0:
                return -1;
        }

        var teacher = await teacherRepository.GetByIdAsync(collegeAddRequest.DeanId.Value);
        if (teacher == null)
        {
            return -1;
        }

        return await collegeRepository.InsertAsync(collegeAddRequest);
    }

    public async Task<int> UpdateCollege(CollegeUpdateRequest collegeUpdateRequest)
    {
        if (collegeUpdateRequest.Id <= 0)
        {
            return -1;
        }

        switch (collegeUpdateRequest.DeanId)
        {
            case null:
                return await collegeRepository.UpdateAsync(collegeUpdateRequest) ? 1 : -1;
            case <= 0:
                return -1;
        }

        var teacher = await teacherRepository.GetByIdAsync(collegeUpdateRequest.DeanId.Value);
        if (teacher == null)
        {
            return -1;
        }

        return await collegeRepository.UpdateAsync(collegeUpdateRequest) ? 1 : -1;
    }

    public async Task<int> DeleteCollegeByIds(List<long> ids)
    {
        return await collegeRepository.DeleteByIdsAsync(ids) ? 1 : -1;
    }
}